package org.example;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@RestController
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }

    @Data
    static class Dto {
        String fileid;
        String type;
    }

    @PostMapping("list")
    public Map<String, Object> aa(@RequestBody Dto dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("result", 0);
        map.put("message", "操作成功");
        List<Finfo> list = new ArrayList<>();
        if ("18790564564564654-5".equals(dto.getFileid())) {
            list.add(new Finfo("气象预报111", "18790564564564654-1", "2", 0, 1, 1));
            list.add(new Finfo("气象预报222", "18790564564564654-2", "2", 0, 1, 1));
            list.add(new Finfo("河道-80065301.dem111", "18790564564564654-3", "1", 280, 1, 1));
            list.add(new Finfo("河道-80065301.dem222", "18790564564564654-4", "1", 280, 1, 1));
        } else {
            list.add(new Finfo("气象预报", "18790564564564654-5", "2", 0, 1, 1));
            list.add(new Finfo("河道-80065301.dem", "18790564564564654-6", "1", 280, 1, 1));
        }

        map.put("data", list);
        return map;
    }

    @GetMapping("tag")
    public Map<String, Object> tag(String fileid) {
        Map<String, Object> map = new HashMap<>();
        map.put("result", 0);
        map.put("message", "操作成功");
        Map<String, String> map1 = new HashMap<>();
        map1.put("mtnm", "XXXX1");
        map1.put("mtcd", "18790564564564653");
        map.put("data", map1);
        return map;
    }

    @Data
    static class Dto2 {
        String fileid;
        Integer start;
        Integer size;
    }

    @Value("${fileBase}")
    String fileBase;

    @PostMapping("fstream")
    public ResponseEntity<InputStreamResource> downloadFile2(@RequestBody Dto2 dto2) throws FileNotFoundException {
        // 假设文件存储在 D:/download 目录下
        String fileid = dto2.getFileid();
        File file = new File(fileBase + "/" + fileid);
        if (!file.exists()) {
            return ResponseEntity.notFound().build();
        }
        InputStreamResource inputStreamResource = new InputStreamResource(new FileInputStream(file));
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"2.jpg\"").body(inputStreamResource);
    }


}